package com.iszhouhua.blog.controller.vue;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iszhouhua.blog.common.util.Result;
import com.iszhouhua.blog.model.Article;
import com.iszhouhua.blog.model.Category;
import com.iszhouhua.blog.model.vo.ArticleVo;
import com.iszhouhua.blog.service.ArticleTagService;
import com.iszhouhua.blog.service.CategoryService;
import com.iszhouhua.blog.service.TagService;
import com.iszhouhua.blog.service.vue.BlogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@CrossOrigin
@RequestMapping("blog")
public class BlogController {

  @Autowired
  private BlogService blogService;

  @Autowired
  private TagService tagService;

  @Autowired
  private CategoryService categoryService;

  @Autowired
  private ArticleTagService articleTagService;
  /**
   * 分页查询全部文章
   * @param current
   * @param size
   * @return
   */
  @GetMapping("/queryAllBlog")
  public Result queryAllBlog(
          @RequestParam(value = "current", required = false, defaultValue = "1") int current,
          @RequestParam(value = "size", required = false, defaultValue = "10") int size

  ){
    try{
      QueryWrapper<Article> wrapper = new QueryWrapper<>();
      Page<ArticleVo> page = new Page<>(current, size);
      IPage<ArticleVo> articleIPage = blogService.queryAllBlog(page);

      for (ArticleVo articleVo : articleIPage.getRecords()) {
        Category category = categoryService.getById(articleVo.getCategoryId());
        articleVo.setCategory(category);
      }
      return new Result( 20000,"成功" ,articleIPage);
    }catch (Exception e){
      return new Result( 500,"查询失败" ,null);
    }

  }


  /**
   * 根据id查询文章
   * @param id
   * @return
   */
  @GetMapping("/queryBlogById")
  public Result queryBlogById(
          @RequestParam(value = "id", required = true) Integer id
  ){
    Article article = blogService.queryBlogById(id);
    Category category = categoryService.getById(article.getCategoryId());

    article.setCategory(category);
    return new Result( 20000,"成功" ,article);
  }

  /**
   * 根据关键字查询文章
   * @param key
   * @return
   */
  @GetMapping("/queryBlogByKey")
  public Result queryBlogByKey(
          @RequestParam(value = "current", required = false, defaultValue = "1") int current,
          @RequestParam(value = "size", required = false, defaultValue = "10") int size,
          @RequestParam(value = "key", required = false) String key
  ){
    IPage<Article> page = new Page<>(current, size);
    QueryWrapper<Article> queryWrapper = new QueryWrapper();
    if (StringUtils.isNotBlank(key)){
      queryWrapper.like("title",key).or().like("description",key);
    }
    queryWrapper.orderByDesc("is_top");
    queryWrapper.orderByDesc("update_time");
    IPage<Article> iPage = blogService.page(page, queryWrapper);
    for (Article article : iPage.getRecords()) {
      Category category = categoryService.getById(article.getCategoryId());
      article.setCategory(category);
    }
    return new Result( 20000,"成功" ,iPage);
  }
}
